package com.boarsoft.boar.admin.biz;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.admin.CorpBiz;
import com.boarsoft.boar.sys.entity.SysRole;
import com.boarsoft.boar.sys.entity.SysUser;
import com.boarsoft.boar.sys.entity.UserRole;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("corpBiz")
public class CorpBizImpl extends SimpleBizImpl implements CorpBiz {
	/**
	 * 删除公司
	 * 
	 * @param id-公司id
	 */
	@Override
	@Transactional
	public void delete(String id) {
		this.deleteSysUser(id);
		this.deleteSysRole(id);
		// 由附加的切面执行其它的关联操作，可能的切面有：sourceBizImpl
	}

	@Override
	@Transactional
	public void deleteSysUser(String corpId) {
		StringBuilder sb = new StringBuilder();
		sb.append("delete from ").append(SysUser.class.getName());
		sb.append(" where corpId='").append(corpId).append("'");
		dao.executeUpdate(sb.toString());
	}

	/**
	 * 删除本公司用户在系统中的角色 操作role_user表
	 * 
	 * @param corpId-公司id
	 */
	@Override
	@Transactional
	public void deleteSysRole(String corpId) {
		StringBuilder sb = new StringBuilder();
		sb.append("delete from ").append(SysRole.class.getName());
		sb.append(" where corpId='").append(corpId).append("'");
		dao.executeUpdate(sb.toString());
	}

	/**
	 * 删除本公司用户在系统中的用户角色
	 * 
	 * @param userId-用户id
	 */
	@Override
	@Transactional
	public void deleteUserRole(String userId) {
		StringBuilder sb = new StringBuilder();
		sb.append("delete from ").append(UserRole.class.getName());
		sb.append(" where userId='").append(userId).append("'");
		dao.executeUpdate(sb.toString());
	}
}